<template>
  <div :class="['message', messageClass]">
    <div class="message-bubble">
      <div class="message-content">
        <p
          v-if="message.type === 'ai'"
          v-html="parseMarkdown(message.content)"
          class="markdown-content"
        ></p>
        <div v-else>
          {{ message.content }}
        </div>
        <span v-if="message.isStreaming" class="streaming-cursor">|</span>
      </div>
      <div class="message-time">{{ formatTime(message.timestamp) }}</div>
    </div>
  </div>
</template>

<script setup>
import { marked } from "marked";
import "highlight.js/styles/github.css";
import hljs from "highlight.js";

// 配置marked
marked.setOptions({
  renderer: new marked.Renderer(),
  highlight: function (code, lang) {
    const language = hljs.getLanguage(lang) ? lang : "plaintext";
    return hljs.highlight(code, { language }).value;
  },
  langPrefix: "hljs language-",
  pedantic: false,
  gfm: true,
  breaks: true,
  sanitize: false,
  smartypants: false,
  xhtml: false,
});

const props = defineProps({
  message: {
    type: Object,
    required: true,
  },
});

const messageClass = computed(() => {
  return props.message.type === "user" ? "user-message" : "ai-message";
});

// 解析markdown
const parseMarkdown = (content) => {
  if (!content) return "";
  try {
    return marked.parse(content);
  } catch (error) {
    console.error("Markdown parsing error:", error);
    return content;
  }
};

// 格式化时间
const formatTime = (timestamp) => {
  const date = new Date(timestamp);
  return date.toLocaleTimeString("zh-CN", {
    hour: "2-digit",
    minute: "2-digit",
  });
};
</script>

<style scoped>
.message {
  margin-bottom: 16px;
  display: flex;
}

.user-message {
  justify-content: flex-end;
}

.ai-message {
  justify-content: flex-start;
}

.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 18px;
  position: relative;
}

.user-message .message-bubble {
  background-color: #007aff;
  color: white;
  border-bottom-right-radius: 6px;
}

.ai-message .message-bubble {
  background-color: #ffffff;
  color: #1c1c1e;
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.message-content {
  font-size: 16px;
  line-height: 1.4;
  word-wrap: break-word;
}

.message-time {
  font-size: 12px;
  opacity: 0.6;
  margin-top: 4px;
}

/* 流式输入光标 */
.streaming-cursor {
  animation: blink 1s infinite;
  font-weight: bold;
  color: #000;
}

@keyframes blink {
  0%,
  50% {
    opacity: 1;
  }
  51%,
  100% {
    opacity: 0;
  }
}

/* Markdown样式 */
.markdown-content {
  line-height: 1.6;
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4,
.markdown-content h5,
.markdown-content h6 {
  margin: 16px 0 8px 0;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-content h1 {
  font-size: 1.5em;
}
.markdown-content h2 {
  font-size: 1.3em;
}
.markdown-content h3 {
  font-size: 1.1em;
}

.markdown-content p {
  margin: 8px 0;
}

.markdown-content ul,
.markdown-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.markdown-content li {
  margin: 4px 0;
}

.markdown-content blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid #007aff;
  background-color: rgba(0, 122, 255, 0.05);
  border-radius: 0 6px 6px 0;
}

.markdown-content code {
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 3px;
  padding: 2px 4px;
  font-family: "SF Mono", Menlo, monospace;
  font-size: 0.9em;
}

.markdown-content pre {
  background-color: #f8f8f8;
  border-radius: 6px;
  padding: 12px;
  margin: 8px 0;
  overflow-x: auto;
  border: 1px solid #e1e1e1;
  margin-bottom: 10px;
}

.markdown-content pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}

.markdown-content table {
  border-collapse: collapse;
  margin: 8px 0;
  width: 100%;
}

.markdown-content th,
.markdown-content td {
  border: 1px solid #e1e1e1;
  padding: 8px 12px;
  text-align: left;
}

.markdown-content th {
  background-color: #f8f8f8;
  font-weight: 600;
}

.markdown-content a {
  color: #007aff;
  text-decoration: none;
}

.markdown-content a:hover {
  text-decoration: underline;
}

.markdown-content strong {
  font-weight: 600;
}

.markdown-content em {
  font-style: italic;
}

/* 为AI消息中的markdown内容调整样式 */
.ai-message .markdown-content {
  color: #1c1c1e;
}

.ai-message .markdown-content h1,
.ai-message .markdown-content h2,
.ai-message .markdown-content h3,
.ai-message .markdown-content h4,
.ai-message .markdown-content h5,
.ai-message .markdown-content h6 {
  color: #1c1c1e;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .message-bubble {
    max-width: 85%;
  }
}
</style>

<style>
.markdown-content pre {
  width: 100%;
  background-color: #f8f8f8;
  box-sizing: border-box;
  line-height: 1 !important;
  border-radius: 6px;
  padding: 12px;
  margin: 8px 0;
  overflow-x: auto;
  border: 1px solid #e1e1e1;
  margin-bottom: 10px;
}

.markdown-content pre code {
  width: 100% !important;
  box-sizing: border-box;
  line-height: 1 !important;
  font-size: 14px;
  font-family: "SF Mono", Menlo, monospace;
  color: #1c1c1e;
  text-wrap: wrap;
  word-break: break-all;
  white-space: pre-wrap;
}

.markdown-content.message-content {
  font-size: 14px;
  line-height: 1 !important;
}

.markdown-content thead,
.markdown-content tbody,
.markdown-content tr,
.markdown-content td,
.markdown-content th {
  border: 1px solid #e1e1e1;
  border-collapse: collapse;
  padding: 4px 6px;
  text-align: left;
  /* 去掉表格的边之间的空隙 */
  border-spacing: 0;
} 

.markdown-content table {
  border-spacing: 0;
  border-collapse: collapse;
  font-size: 14px;
  margin-bottom: 10px;
}

.markdown-content p {
  margin-bottom: 10px;
}
</style>
